home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 670 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: clamage@Eng.sun.com (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Enumerated type converted to pointer? Lega
  5. Date: 8 Mar 1996 21:20:40 GMT
  6. Organization: Sun Microsystems Inc.
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4hpuhm$ets@engnews1.Eng.Sun.COM>
  9. References: <4hobji$mco@netlab.cs.rpi.edu>
  10. Reply-To: clamage@Eng.sun.com
  11. NNTP-Posting-Host: taumet.eng.sun.com
  12. Content-Type: text
  13. X-Nntp-Posting-Host: taumet.eng.sun.com
  14. Content-Length: 1599
  15. X-Lines: 51
  16. Originator: clamage@taumet
  17.  
  18. In article mco@netlab.cs.rpi.edu, bstowers@pobox.com (Brad Stowers) writes:
  19. >#include <stdio.h>
  20. >
  21. >typedef enum { mdType1, mdType2, mdType3 } myType;
  22. >
  23. >void FooBar(int x,
  24. >            int* y,
  25. >            myType type = mdType1)
  26. >{
  27. >  printf("x=%i\ny=%p\ntype=%type\n", x, y, type);
  28. >}
  29. >
  30. >main()
  31. >{
  32. >  int x = 10;
  33. >  int* y = &x;
  34. >  FooBar(x, y, mdType2);
  35. >  FooBar(x, mdType1);
  36. >}
  37.  
  38. >  Is it just me, or should the last line
  39. >of the main() function ( FooBar(x, mdType1); ) have caused a type mismatch
  40. >error? 
  41.  
  42. The code is valid (although incorrect) as it stands.
  43.  
  44. > I think I know why it didn't:  mdType1 evaluates to an integer with
  45. >a value of 0, which is a valid value to pass as a pointer.
  46.  
  47. That's about right. In more detail: Parameter y needs an int*, but the
  48. actual parameter is a constant of enumerated type. In an expression, an
  49. enumerator gets promoted to type int (or to a wider type if necessary),
  50. so at this point we have an int constant with value zero. The definition
  51. of "null pointer constant" is an integer constant-expression with value zero,
  52. so mdType1 qualifies for conversion to a null int*.
  53.  
  54. >Obviously, the work-around is simple:
  55. >
  56. >  typedef enum { mdType1 = 1, mdType2, mdType3 } myType;
  57. >
  58. >While this will solve the problem, I really think that it is the compiler's
  59. >job to see that mdType1 does not evaluate to the proper type no matter what
  60. >it's internal representation might be.
  61.  
  62. It isn't the internal representation, but the zero value. This is
  63. yet another C compatibility hack that tends to subvert the type system.
  64.  
  65. ---
  66. Steve Clamage, stephen.clamage@eng.sun.com
  67.  
  68.  
  69.  
  70.  
  71. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your
  72.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  73.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  74.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  75.   Comments? mailto:std-c++-request@ncar.ucar.edu
  76. ]
  77.